package com.gh.springbootrabbitmq.controller;

import com.gh.springbootrabbitmq.config.TtiQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author Eric
 * @Date 2021/7/19 20:04
 * @Version 1.0
 * 发送延迟消息
 */
@RestController
@RequestMapping("ttl")
@Slf4j
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg/{msg}")
    public void sendMsg(@PathVariable String msg){
        log.info("当前时间:{},发送一条消息给两个ttl队列:{}",new Date().toString(),msg);
        rabbitTemplate.convertAndSend(TtiQueueConfig.X_EXCHANGE,"XA","消息来自ttl为10秒的队列"+msg);
        rabbitTemplate.convertAndSend(TtiQueueConfig.X_EXCHANGE,"XB","消息来自ttl为40秒的队列"+msg);

    }

    /**
     * 开始发消息 ttl
     * @param msg
     */
    @GetMapping("sendExpirationMsg/{msg}/{ttlTime}")
    public void sendMsg(@PathVariable String msg,@PathVariable String ttlTime){
        log.info("当前时间:{},发送一条消息给一个时长{}毫秒ttl队列QC:{}",new Date().toString(),ttlTime,msg);
        rabbitTemplate.convertAndSend(TtiQueueConfig.X_EXCHANGE,"XC",msg,message -> {
            //发送消息的延时时长
            message.getMessageProperties().setExpiration(ttlTime);
            return message;
        });

    }
}
